<template>
    <Dialog
      :show="dialogConfig.show"
      :title="dialogConfig.title"
      :buttons="dialogConfig.buttons"
      :showCancel="true"
      @close="dialogConfig.show = false"
        :width="600+'px'"
    >
     <div >
        <el-form
        :model="formData"
        :rules="rules"
        ref="formDataRef"
        label-width="80px"
        @submit.prevent
      >

      <el-form-item  label="预约编号" prop="bookId"  label-width="120px">
          <el-input disabled v-model="formData.borrowId" />
        </el-form-item>
        <el-form-item label="图书编号" prop="bookId"  label-width="120px">
          <el-input v-model="formData.bookId" />
        </el-form-item>

        <el-form-item label="学工号" prop="jobNumber"  label-width="120px">
          <el-input v-model="formData.jobNumber" />
        </el-form-item>

    </el-form>
     </div>
    </Dialog>
  </template>

  <script setup>
  import { ref, reactive, getCurrentInstance, nextTick } from "vue";
  const { proxy } = getCurrentInstance();

  const api = {
    saveBorrowRecord:"/borrowRecordInfo/saveBorrowRecord",

  };


  const dialogConfig = ref({
    show: false,
    title: "新增图书",
    buttons: [
      {
        type: "primary",
        text: "确定",
        click: (e) => {
          submitForm();
        },
      },
    ],
  });

  const formData = ref({});
  const formDataRef = ref();


  const rules = {
    bookId: [{ required: true, message: "请输入要借阅的图书编号" }],
    jobNumber: [{ required: true, message: "请输入借阅人的学工号" }],

  };

  const showEdit = (data) => {
    dialogConfig.value.show = true;
    nextTick(() => {
      formDataRef.value.resetFields();
      if (data.borrowId == null) {
        dialogConfig.value.title = "新增借阅记录";
      } else {
        dialogConfig.value.title = "修改借阅记录";
        let editData = Object.assign({}, data);
        formData.value = Object.assign({}, editData);
      }
    });
  };

  defineExpose({
    showEdit,
  });

  const emit = defineEmits(["reload"]);
  const submitForm = async () => {
    formDataRef.value.validate(async (valid) => {
      if (!valid) {
        return;
      }
      let params = {};
      Object.assign(params, formData.value);

      let result = await proxy.Request({
        url: api.saveBorrowRecord,
        params,
      });
      if (!result) {
        return;
      }
      dialogConfig.value.show = false;
      proxy.Message.success("借阅成功可在【图书借还】下查看详情");
      emit("reload");
    });
  };
  </script>

  <style lang="scss" scoped>
  .check-span-item {
    float: left;
    margin-right: 10px;
    line-height: 20px;
  }
  </style>
